<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>

    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f7fc;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .login-container {
            background-color: #ffffff;
            border-radius: 8px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            padding: 30px;
            width: 100%;
            max-width: 400px;
        }

        h2 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }

        .input-group {
            margin-bottom: 20px;
        }

        .input-group label {
            font-size: 14px;
            color: #666;
            margin-bottom: 5px;
            display: block;
        }

        .input-group input {
            width: 100%;
            padding: 10px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .input-group input:focus {
            border-color: #4CAF50;
            outline: none;
        }

        .submit-group {
            text-align: center;
        }

        .submit-group input {
            width: 100%;
            padding: 10px;
            font-size: 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .submit-group input:hover {
            background-color: #45a049;
        }

        .output {
            text-align: center;
            margin-top: 20px;
            color: #d9534f;
        }

        .output.success {
            color: #5bc0de;
        }
    </style>
</head>

<body>
    <div class="login-container">
        <h2>登录</h2>
        <form id="dataForm">
            <div class="input-group">
                <label for="username">账号</label>
                <input type="text" id="username" name="username" placeholder="请输入账号">
            </div>

            <div class="input-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入密码">
            </div>

            <div class="submit-group">
                <input type="submit" value="登录" id="login">
            </div>
        </form>

        <div id="output" class="output">登录消息</div>
    </div>

    <script>
        const loginForm = document.getElementById('dataForm');

        loginForm.addEventListener('submit', async function (event) {
            event.preventDefault();  // 阻止表单的默认提交行为

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;

            // 检查账号密码是否为空
            if (!username || !password) {
                document.getElementById("output").textContent = "账号和密码不能为空";
                document.getElementById("output").classList.remove('success');
                return;
            }

            try {
                const response = await fetch("http://124.221.110.118:3000/login", {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify({ username, password })
                });

                // 获取返回的 JSON 数据
                const result = await response.json();

                // 判断请求是否成功
                if (response.status === 200) {
                    console.log(result.message);
                    document.getElementById("output").textContent = "登录成功！3秒后跳转";
                    document.getElementById("output").classList.add('success');
                    setTimeout(() => {
                        window.location.href = "./login_on.html";  // 跳转到登录后的页面
                    }, 3000);
                }else if(response.status === 201){
                    document.getElementById("output").textContent = result.error || "密码和账号不匹配(密码错误)";
                    document.getElementById("output").classList.remove('success');
                } 
                else if (response.status === 400) {
                    // 登录失败时输出错误信息
                    document.getElementById("output").textContent = result.error || "登录失败！";
                    document.getElementById("output").classList.remove('success');
                } else {
                    // 处理其他状态码
                    document.getElementById("output").textContent = "登录失败，未知错误！";
                    document.getElementById("output").classList.remove('success');
                }

            } catch (error) {
                console.error("请求失败", error);
                document.getElementById("output").textContent = "网络错误，登录失败！";
                document.getElementById("output").classList.remove('success');
            }
        });
    </script>
</body>

</html>
